/*
 * Copyright (c) 2007 Aleksey Nikiforov
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'Aleksey Nikiforov' nor the names of other contributors 
 *   may be used to endorse or promote products derived from this software 
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.lex.input.action;


import org.lex.input.effects.filter.InputFilter2d;
import org.lex.input.mouse.MouseManager;

import com.jme.input.action.InputActionEvent;

public class MouseScrollAction {

	private MouseManager mouseManager;
	private InputFilter2d inputFilter;
	private float border;
	
	private int width;
	private int height;
	
	private String scrollUpCursor;
	private String scrollLeftCursor;
	private String scrollDownCursor;
	private String scrollRightCursor;
	private String scrollUpLeftCursor;
	private String scrollDownLeftCursor;
	private String scrollDownRightCursor;
	private String scrollUpRightCursor;
	
	public MouseScrollAction(MouseManager mouseManager,
			InputFilter2d inputFilter)
	{
		this.mouseManager = mouseManager;
		this.inputFilter = inputFilter;
		
		width = (int) mouseManager.getWidth();
		height = (int) mouseManager.getHeight();
	}
	
	public void reinit(int width, int height) {
		this.width = width;
		this.height = height;
	}
	
	public boolean performAction(InputActionEvent evt) {
		float x = mouseManager.getMousePosition().x;
		float y = mouseManager.getMousePosition().y;
		
		// think of this variale as two ternary digits
		int scrollDirection = 0;
		
		if ( x <= border) {
			inputFilter.feedInputX(-evt.getTime());
			scrollDirection = 3; // second digit = 1
		} else if ( x >= width - 1 - border) {
			inputFilter.feedInputX(evt.getTime());
			scrollDirection = 6; // second digit = 2
		}
		
		if ( y <= border) {
			inputFilter.feedInputY(-evt.getTime());
			scrollDirection += 1; // first digit = 1
		} else if ( y >= height - 1 - border) {
			inputFilter.feedInputY(evt.getTime());
			scrollDirection += 2; // first digit = 2
		}
		
		switch(scrollDirection) {
		case 1: mouseManager.useCursor(scrollDownCursor); break;
		case 2: mouseManager.useCursor(scrollUpCursor); break;
		case 3: mouseManager.useCursor(scrollLeftCursor); break;
		case 4: mouseManager.useCursor(scrollDownLeftCursor); break;
		case 5: mouseManager.useCursor(scrollUpLeftCursor); break;
		case 6: mouseManager.useCursor(scrollRightCursor); break;
		case 7: mouseManager.useCursor(scrollDownRightCursor); break;
		case 8: mouseManager.useCursor(scrollUpRightCursor); break;
		}
		
		return (scrollDirection != 0);
	}

	/**
	 * @return the border
	 */
	public float getBorder() {
		return border;
	}

	/**
	 * @param border the border to set
	 */
	public void setBorder(float border) {
		this.border = border;
	}

	public void setScrollDownCursor(String scrollDownCursor) {
		this.scrollDownCursor = scrollDownCursor;
	}

	public void setScrollDownLeftCursor(String scrollDownLeftCursor) {
		this.scrollDownLeftCursor = scrollDownLeftCursor;
	}

	public void setScrollDownRightCursor(String scrollDownRightCursor) {
		this.scrollDownRightCursor = scrollDownRightCursor;
	}

	public void setScrollLeftCursor(String scrollLeftCursor) {
		this.scrollLeftCursor = scrollLeftCursor;
	}

	public void setScrollRightCursor(String scrollRightCursor) {
		this.scrollRightCursor = scrollRightCursor;
	}

	public void setScrollUpCursor(String scrollUpCursor) {
		this.scrollUpCursor = scrollUpCursor;
	}

	public void setScrollUpLeftCursor(String scrollUpLeftCursor) {
		this.scrollUpLeftCursor = scrollUpLeftCursor;
	}

	public void setScrollUpRightCursor(String scrollUpRightCursor) {
		this.scrollUpRightCursor = scrollUpRightCursor;
	}

}
